package park.job;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import park.record.ExitRecord;
import park.record.LatestRecordParam;

public class TaskExitJob implements Job {
	private static Logger logger = Logger.getLogger(TaskExitJob.class);

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {

		Long timeMark = 0l; // timemark
		String jobId;
		logger.info("starting exitJob: " + (jobId = context.getJobDetail().getKey().getName()) + " at time mark:"
				+ timeMark);

		JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
		ExitRecord exit = (ExitRecord) jobDataMap.get("exitRecord");

		ParkInterfaceCollection pc = ParkInterfaceCollection.getInstance();

		// TODO: 7/4/17 同样这里你new的vehicleInfo是不是可以用exitRecord代替 || 已经改了
		try {
			try {
				Thread.sleep(1000 * 6);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			String rsCheckFree = pc.postFunc("checkFreeVehicle", exit);
			logger.info("job:" + jobId + "|  checkFreeVehicle response " + (System.currentTimeMillis() - timeMark)
					+ " ms after time mark");
			logger.info("job:" + jobId + "|  response of checkFreeVehicle is : " + rsCheckFree.toString());

			LatestRecordParam latest = new LatestRecordParam();
			latest.setPlateNo(exit.getPlateNo());
			String[] entryids = JobExecutor.entryIds.toArray(new String[JobExecutor.entryIds.size()]);
			latest.setEntryIds(entryids);
			latest.setTerminalId(exit.getTerminalId());

			latest.setTollCollectorName(exit.getTollCollectorName());
			String rsgetLatestEntryRecord = pc.postFunc("getNearestEntryRecord", latest);
			logger.info("job:" + jobId + "|  getNearestEntryRecord response " + (System.currentTimeMillis() - timeMark)
					+ " ms after time mark");
			logger.info(
					"job:" + jobId + "|  response of getNearestEntryRecord is : " + rsgetLatestEntryRecord.toString());
			String rscheckMonthPayVeicle = pc.postFunc("checkMonthlyPayVehicle", exit);
			logger.info("job:" + jobId + "|  checkMonthlyPayVehicle response " + (System.currentTimeMillis() - timeMark)
					+ " ms after time mark");
			logger.info(
					"job:" + jobId + "|  response of checkMonthlyPayVehicle is : " + rscheckMonthPayVeicle.toString());
			checkResponse("getNearestEntryRecord", rsgetLatestEntryRecord, "entryId");

			exit.setExitTime(System.currentTimeMillis());
			exit.setTradeTime(System.currentTimeMillis() - 1000);
			exit.setExitId(exit.getEntryId() + 10);
			String rsExitRecord = pc.postFunc("saveExitTradingRecord", exit);
			logger.info("job:" + jobId + "|  saveExitTradingRecord response " + (System.currentTimeMillis() - timeMark)
					+ " ms after time mark");
			logger.info("job:" + jobId + "|  response of saveExitTradingRecord is : " + rsExitRecord.toString());
			checkResponse("saveExitTradingRecord", rsExitRecord, "exitRecordId");
		} catch (Exception e) {
			logger.info("job:" + jobId + "| caught exception", e);
		}
	}

	private void checkResponse(String url, String res, String key) {
		if (!res.contains(key)) {
			logger.error("!!!!res of " + res + " has no key of " + key);

		} else {
			logger.info("res: " + res + " has key : " + key);
		}

	}
}
